var api_url = 'http://message.com/';
var version = '1.0020300147';
var libs = {
    ajax: function (url, data, success, error) {
        var d = {
            user: localStorage.mUser,
            sid: localStorage.mSid
        }
        $.extend(d, data)
        $.ajax({
            url: api_url + url,
            data: d,
            sync:true,
            type: "POST",
            dataType: 'Json',
            success: function (result) {
                if(result.status == 2) {
                    localStorage.clear();
                    // top.location.href = 'MessageBoard-master/application/index/view/index/index.html'
                    top.location.href = '/'
                }
                if (success && $.isFunction(success)) {
                    success(result)
                }
            },
            error: function (result) {
                if (error && $.isFunction(error)) {
                    error(result)
                }
            }
        })
    }
}

// function GetQueryString(name) {
//     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//     var r = window.location.search.substr(1).match(reg);
//     if (r != null) return unescape(r[2]);
//     return null;
// }
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
    context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}

Date.prototype.format = function (a) {
    var c = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds()
    };
    if (/(y+)/.test(a)) {
        a = a.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))
    }
    for (var b in c) {
        if (new RegExp("(" + b + ")").test(a)) {
            a = a.replace(RegExp.$1, (RegExp.$1.length == 1) ? (c[b]) : (("00" + c[b]).substr(("" + c[b]).length)))
        }
    }
    return a
};

/************** 翻页 *************/

Vue.component('page-turn', {
    props: ['list'],
    template: '<div class="pageTurn"><span>共 {{sumpage}} 页，当前第 {{ curpage }} 页 </span> <span class="input-button" @click="pageturn(\'F\')">首页</span><span class="input-button" @click="pageturn(\'P\')">上一页</span><input type="number" autocomplete="off" class="input-text input-50 center" min=1 :max=sumpage v-model.number="curpage"><span class="input-button" @click="pageturn(\'G\')">GO</span><span class="input-button" @click="pageturn(\'N\')">下一页</span><span class="input-button" @click="pageturn(\'L\')">末页</span></div>',
    data: function () {
        return {
            curpage: 1,
            sumpage: 0,
            list: this.lsit
        }
    },
    watch: {
        list: function (val) {
            this.sumpage = Math.ceil(this.list.length / 100);
            this.$emit('update', this.curList, this.curpage)
        }
    },
    computed: {
        curList: function () {
            var arr = [];
            var sp = (this.curpage - 1) * 100
            var ep = this.curpage * 100 > this.list.length ? this.list.length : this.curpage * 100
            for (var k = sp; k < ep; k++) {
                arr.push(this.list[k])
            }
            return arr;
        }
    },
    methods: {
        pageturn: function (t) {
            if (t == 'F') {
                this.curpage = 1;
            }
            if (t == 'L') {
                this.curpage = this.sumpage;
            }
            if (t == 'P') {
                this.curpage = this.curpage - 1 < 1 ? 1 : this.curpage - 1
            }
            if (t == 'N') {
                this.curpage = this.curpage + 1 > this.sumpage ? this.sumpage : this.curpage + 1
            }
            if (t == 'G') {
                var page = this.curpage
                this.curpage = page > this.sumpage ? this.sumpage : page < 1 ? 1 : page;
            }
            this.$emit('update', this.curList, this.curpage)
        }
    }
})

/************** 翻页2 *************/

Vue.component('page-turn2', {
    props: ['page', 'pagesum', 'sum'],
    template: '<div class="pageTurn"><span>共 {{sum}} 条 {{pagesum}} 页，当前第 {{ curpage }} 页 </span> <span class="input-button" @click="pageturn(\'F\')">首页</span><span class="input-button" @click="pageturn(\'P\')">上一页</span><input type="number" autocomplete="off" class="input-text input-50 center" min=1 :max=pagesum v-model.number="curpage"><span class="input-button" @click="pageturn(\'G\')">GO</span><span class="input-button" @click="pageturn(\'N\')">下一页</span><span class="input-button" @click="pageturn(\'L\')">末页</span></div>',
    data: function () {
        return {
            curpage: 1
        }
    },
    methods: {
        pageturn: function (t) {
            this.curpage = this.page
            if (t == 'F') {
                this.curpage = 1;
            }
            if (t == 'L') {
                this.curpage = this.pagesum;
            }
            if (t == 'P') {
                this.curpage = this.curpage - 1 < 1 ? 1 : this.curpage - 1
            }
            if (t == 'N') {
                this.curpage = this.curpage + 1 > this.pagesum ? this.pagesum : this.curpage + 1
            }
            if (t == 'G') {
                var page = this.curpage
                this.curpage = page > this.pagesum ? this.pagesum : page < 1 ? 1 : page;
            }
            this.$emit('update', this.curpage)
        }
    }
})


Vue.component('yi-menu', {
    props:['index'],
    template: '<div><a :href="item.url" v-for="(item, key) in list" :class = "{\'on\': key == index}">{{item.title}}&nbsp;</a></div>',
    data: function () {
        return {
            list : [
            {
                title : '监控概况',
                url : 'getRate.html?_v=' + version,
            },
            // {
            //     title : '代理支付宝监控',
            //     url : 'dlAisle.html?_v=' + version,
            // },{
            //     title : '代理微信监控',
            //     url : 'dlwAisle.html?_v=' + version
            // },
            {
                title : '代理银行卡监控',
                url : 'dlbankAisle.html?_v=' + version
            },{
                title : '银行卡预付金代付管理',
                url : 'dlbankAislePayment.html?_v=' + version
            },{
                title : '通道管理',
                url : 'getPaywayList.html?id=3'
            },{
                title : '封停记录',
                url : 'PaywayForbidden.html?_v=' + version
            },{
                title: '账号录入记录',
                url: 'PaywayEroor.html?_v=' + version
            },{
                title: '代付卡管理',
                url: 'dlbankPayment.html?_v=' + version
            },{
                title: '预付金卡管理',
                url: 'dlbankEarnest.html?_v=' + version
            }],
            index : this.index
        }    
    },
    methods: { }
})

Vue.component('input-submit', {
    props:['text','issend'],
    template: '<button class="input-button input-submit" @click="send" :disabled="isSend"><img src="loading.svg" v-if="isSend"><span v-else>{{text ||"查询"}}</span></button>',
    data: function () {
        return {
            isSend : this.issend
        }    
    },
    watch : {
        issend : function(val){
            this.isSend = val  
        }
    },
    methods: {
        send: function () {
             this.$emit('send', true)
        }  
    }
})

Vue.component('sort-th',{
    props:['list','item'],
    template:'<td><a @click="sort"><slot></slot></a></td>',
    data : function(){
        return {
            list : this.list,
            item : this.item,
            reverse : false        
        }
        
    },
    methods : {
        sort : function(){
            var _self = this;
            if(_self.item.indexOf('/') > -1){
                var item = _self.item.split('/')
                this.list.sort(function(a, b){
                    if(_self.reverse){
                        return (a[item[1]] == 0 ? 0 : (a[item[0]] / a[item[1]]))-(b[item[1]] == 0 ? 0 : (b[item[0]] / b[item[1]]))  
                    }else{
                        return (b[item[1]] == 0 ? 0 : (b[item[0]] / b[item[1]]))-(a[item[1]] == 0 ? 0 : (a[item[0]] / a[item[1]]))
                    }
                }) 
            }else{
                this.list.sort(function(a, b){
                    if(_self.reverse){
                        return a[_self.item] - b[_self.item]  
                    }else{
                        return b[_self.item] - a[_self.item]   
                    }
                })    
            }
            this.reverse = !this.reverse
            this.$emit('update', this.list)
        }  
    }
})

Vue.component('chart-time',{
    props:['timevalue'],
    template: '<span><button v-for="item in time" :class="{\'on\': timeValue == item.v}" @click="send(item.v)">{{item.t}}</button></span>',
    data: function () {
        return {
            time : [{
                t : '当日',
                v : 't'
            },{
                t : '昨日',
                v : 'y'    
            },{
                t : '七日',
                v : 'w'    
            },{
                t : '当月',
                v : 'm'
            }],
            timeValue : this.timevalue
        }    
    },
    methods: {
        send: function (value) {
            this.timeValue = value
            this.$emit('update', value)
        }  
    }   
})

Vue.component('chart-time2',{
    props:['timevalue'],
    template: '<span><button v-for="item in time" :class="{\'on\': timeValue == item.v}" @click="send(item.v)">{{item.t}}</button></span>',
    data: function () {
        return {
            time : [{
                t : '昨日',
                v : 'y'    
            },{
                t : '七日',
                v : 'w'    
            },{
                t : '当月',
                v : 'm'
            }],
            timeValue : this.timevalue
        }    
    },
    methods: {
        send: function (value) {
            this.timeValue = value
            this.$emit('update', value)
        }  
    }   
})

Vue.component('chart-type',{
    props:['typevalue'],
    template: '<span><button v-for="item in type" :class="{\'on\': typeValue == item.v}" @click="send(item.v)">{{item.t}}</button></span>',
    data: function () {
        return {
            type : [{
                t : '代理',
                v : 'a'
            },{
                t : '商户',
                v : 't'    
            }],
            typeValue : this.typevalue
        }    
    },
    methods: {
        send: function (value) {
            this.typeValue = value
            this.$emit('update', value)
        }  
    }   
})

Vue.component('synchronize-cell',{
    props:['item'],
    template: `<div>
                    <div class="item" v-if="item.agent_name">{{item.agent_name}}</div>
                    <div class="item" v-if="item.name">
                        <span>{{item.name}}-{{item.username}}</span>
                        <span v-if="item.state === 0">开工</span>
                        <span v-if="item.state === 1">停工</span>
                        <span>余额 : {{item.quota}}</span>
                        <span>信誉额度 : {{item.quota_credit}}</span>
                        <span>今日奖金 : {{item.uncertain_yj}}</span>
                        <span>可提奖金 : {{item.available_yj}}</span>
                        <span>今日佣金 : {{item.uncertain_tc}}</span>
                        <span>可提佣金 : {{item.available_tc}}</span>
                        <span v-for="fee in item.fee">{{fee.pay_type_name}} : {{fee.fee}}</span>
                    </div>
                    <template v-if="item.group.length > 0">
                        <synchronize-cell v-for="(group, index) in item.group" :item="group" :key="index"></synchronize-cell>
                    </template>
                </div>`,
    data: function () {
        return { }
    },
    methods: {

    }
})

Vue.filter('tofixed', function(value){
    if(value == '-') return '-'
    if(value || value == 0 || value != '-'){
        return ((value * 1).toFixed(2) +'').replace(/(\d)(?=(\d{3})+\.)/g, '$1,')      
    }    
})
Vue.filter('wan', function(value){
    if(value) {
        let val = value / 10000
        return `${val.toFixed(2)}万`
    }
})